//
//  OADTitleHeader.m
//  OASDK_Works
//
//  Created by ChenJie on 5/21/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTitleHeader.h"
#import "OASDK.h"
#import "OADTitleHeaderField.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    FieldContainer,
    Separator,
    FieldFrames,
};

@interface OADTitleHeader ()

@property (nonatomic, strong) UIView *fieldContainer;
@property (nonatomic, strong) UIView *separator;
@property (nonatomic, strong) NSMutableArray<OADTitleHeaderField *> *fields;

@end

@implementation OADTitleHeader

#pragma mark - Lifecycle methods

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = OARandomColor();
        
        [self addSubview:self.fieldContainer];
        [self addSubview:self.separator];
    }
    return self;
}

#define kSeparatorHeight 2

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.fieldContainer.frame = [self.viewModel.layouts[@(FieldContainer)] CGRectValue];
    self.separator.frame = [self.viewModel.layouts[@(Separator)] CGRectValue];
    NSArray *fieldFrames = self.viewModel.layouts[@(FieldFrames)];
    
    for (NSInteger i = 0; i < self.fields.count; i++) {
        OADTitleHeaderField *field = self.fields[i];
        field.frame = [fieldFrames[i] CGRectValue];
    }
    
    [self oa_showBounds];
}

#pragma mark - Public methods

/* dynamic viewHeight */
+ (CGFloat)heightWithViewWidth:(CGFloat)viewWidth
                     viewModel:(OADTitleHeaderViewModel *)viewModel {
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    CGFloat leftMark = 0;
    
    //
    CGFloat headerHeight = 0;
    CGFloat flexWidth = viewWidth - viewModel.totalBaseWidth;
    
    NSMutableArray *fieldFrames = [NSMutableArray array];
    for (NSInteger i = 0; i < viewModel.fields.count; i++) {
        OADTitleHeaderFieldViewModel *fieldViewModel = viewModel.fields[i];
        
        CGFloat fieldFlexWidth = 0;
        if (viewModel.totalFlex != 0) {
            fieldFlexWidth = flexWidth * fieldViewModel.flex / viewModel.totalFlex;
        }
        CGFloat fieldWidth = fieldViewModel.width + fieldFlexWidth;
        
        CGFloat fieldHeight = [OADTitleHeaderField heightWithViewWidth:fieldWidth viewModel:fieldViewModel];
        headerHeight = MAX(headerHeight, fieldHeight);
        
        CGRect fieldFrame = CGRectMake(leftMark, 0, fieldWidth, fieldHeight);
        [fieldFrames addObject:[NSValue valueWithCGRect:fieldFrame]];
        
        leftMark += fieldWidth;
    }
    
    for (NSInteger i = 0; i < fieldFrames.count; i++) {
        NSValue *fieldFrameValue = fieldFrames[i];
        CGRect fieldFrame = [fieldFrameValue CGRectValue];
        fieldFrame.size.height = headerHeight;
        fieldFrames[i] = [NSValue valueWithCGRect:fieldFrame];
    }
    
    layouts[@(FieldFrames)] = fieldFrames;
    
    //
    CGRect fieldContainerFrame = CGRectMake(0, topMark, viewWidth, headerHeight);
    layouts[@(FieldContainer)] = [NSValue valueWithCGRect:fieldContainerFrame];
    topMark += headerHeight;
    
    //
    CGRect separatorFrame = CGRectMake(0, topMark, viewWidth, kSeparatorHeight);
    layouts[@(Separator)] = [NSValue valueWithCGRect:separatorFrame];
    
    viewModel.layouts = layouts;
    
    return CGRectGetMaxY(separatorFrame);
}

/* static viewHeight */
+ (void)layoutWithViewSize:(CGSize)viewSize
                 viewModel:(OADTitleHeaderViewModel *)viewModel {
    
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    
    CGFloat topMark = 0;
    CGFloat leftMark = 0;
    CGFloat headerHeight = viewSize.height - kSeparatorHeight;
    
    //
    CGRect fieldContainerFrame = CGRectMake(0, topMark, viewSize.width, headerHeight);
    layouts[@(FieldContainer)] = [NSValue valueWithCGRect:fieldContainerFrame];
    topMark += headerHeight;
    
    //
    CGRect separatorFrame = CGRectMake(0, topMark, viewSize.width, kSeparatorHeight);
    layouts[@(Separator)] = [NSValue valueWithCGRect:separatorFrame];
    
    
    // fields
    CGFloat flexWidth = viewSize.width - viewModel.totalBaseWidth;
    NSMutableArray *fieldFrames = [NSMutableArray array];
    for (NSInteger i = 0; i < viewModel.fields.count; i++) {
        OADTitleHeaderFieldViewModel *fieldViewModel = viewModel.fields[i];
        
        CGFloat fieldFlexWidth = 0;
        if (viewModel.totalFlex != 0) {
            fieldFlexWidth = flexWidth * fieldViewModel.flex / viewModel.totalFlex;
        }
        CGFloat fieldWidth = fieldViewModel.width + fieldFlexWidth;
        CGSize fieldSize = CGSizeMake(fieldWidth, headerHeight);
        [OADTitleHeaderField layoutWithViewSize:fieldSize viewModel:fieldViewModel];
        
        CGRect fieldFrame = CGRectMake(leftMark, 0, fieldSize.width, fieldSize.height);
        [fieldFrames addObject:[NSValue valueWithCGRect:fieldFrame]];
        
        leftMark += fieldWidth;
    }
    layouts[@(FieldFrames)] = fieldFrames;
    
    viewModel.layouts = layouts;
}

#pragma mark - Private methods

- (void)buildFieldsWithViewModel:(OADTitleHeaderViewModel *)viewModel {
    [self.fields removeAllObjects];
    for (UIView *subview in self.fieldContainer.subviews) {
        [subview removeFromSuperview];
    }
    
    for (OADTitleHeaderFieldViewModel *fieldViewModel in viewModel.fields) {
        OADTitleHeaderField *field = [[OADTitleHeaderField alloc] init];
        field.viewModel = fieldViewModel;
        [self.fields addObject:field];
        [self.fieldContainer addSubview:field];
    }
}

#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(OADTitleHeaderViewModel *)viewModel {
    _viewModel = viewModel;
    
    //
    [self buildFieldsWithViewModel:self.viewModel];
}

#pragma mark - Getter

OAGetView(fieldContainer, ^(UIView *view){
    view.backgroundColor = OARandomColor();
})

OAGetView(separator, ^(UIView *view){
    view.backgroundColor = OARandomColor();
})

- (NSMutableArray<OADTitleHeaderField *> *)fields {
    if (!_fields) {
        _fields = [NSMutableArray array];
    }
    return _fields;
}

#pragma mark - Memory management methods

- (void)dealloc {
    OALogFunction
}

@end
